#ifndef REPLYMODEL_H
#define REPLYMODEL_H

#include <QAbstractTableModel>
#include "U8Reply.pb.h"

using namespace Entity;

class ReplyModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit ReplyModel(QObject *parent = nullptr):
        QAbstractTableModel(parent){}

    void setReply(const U8::Reply &reply)
    {
        beginResetModel();
        model=reply;
        endResetModel();
    }

    void clear()
    {
        beginResetModel();
        model.Clear();
        endResetModel();
    }

    int rowCount(const QModelIndex &parent=QModelIndex()) const
    {
        Q_UNUSED(parent)
        return model.result_size();
    }

    int columnCount(const QModelIndex &parent=QModelIndex()) const
    {
        Q_UNUSED(parent)
        return model.column_size();
    }

    QVariant data(const QModelIndex &index,
                  int role= Qt::DisplayRole) const
    {
        // 只读模型
        if(role == Qt::DisplayRole)
        {
            U8::ResultItem row=model.result(index.row());
            std::string result=row.item(index.column());
            return QString::fromStdString(result);
        }
        else
            return QVariant();
    }

    QVariant headerData(int section,
                        Qt::Orientation orientation=Qt::Horizontal,
                        int role=Qt::DisplayRole) const
    {
        if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
        {
            std::string columnHeader=model.column(section);
            return QString::fromStdString(columnHeader);
        }
        else
            return QVariant();
    }

private:
    U8::Reply model;
};

#endif // REPLYMODEL_H
